xquery version "1.0";

module namespace addf="http://www.example.com/addf";

(: This module contains some generic functions which are used in the other modules and scripts :)

declare function addf:substring-before-if-contains($string1, $string2)
{
    if (contains($string1, $string2)) then substring-before($string1, $string2)
    else $string1
};


declare function addf:readableDate($string as xs:string) as xs:string {
    let $year := substring-before($string, '-')
    let $rest := substring-after($string, '-')
    let $month := substring-before($rest, '-')
    let $day := substring-after($rest, '-')
    return concat($day, '/', $month, '/', $year)
};

(: Both functions below are from the http://www.functx.com namespace:)

declare function addf:substring-after-last 
  ( $arg as xs:string? ,
    $delim as xs:string )  as xs:string {
       
   replace ($arg,concat('^.*',addf:escape-for-regex($delim)),'')
 };
 
declare function addf:escape-for-regex 
  ( $arg as xs:string? )  as xs:string {
       
   replace($arg,
           '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')
 };
 
 declare function addf:word-count 
  ( $arg as xs:string? )  as xs:integer {
       
   count(tokenize($arg, '\W+')[. != ''])
 };
